//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.basic.action;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.basic.annotation.LogAnn;
import net.mingsoft.basic.biz.IAppBiz;
import net.mingsoft.basic.constant.e.BusinessTypeEnum;
import net.mingsoft.basic.constant.e.CookieConstEnum;
import net.mingsoft.basic.entity.AppEntity;
import net.mingsoft.basic.entity.ManagerEntity;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.basic.util.MapCacheUtil;
import net.mingsoft.basic.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

@Api(
        tags = {"后端-基础接口"}
)
@Controller
@RequestMapping({"/${ms.manager.path}/basic/app"})
public class AppAction extends BaseAction {
    @Autowired
    private IAppBiz appBiz;

    public AppAction() {
    }

    @GetMapping({"/app"})
    @RequiresPermissions({"basic:app:view"})
    public String app(HttpServletRequest request) {
        return "/WEB-INF/manager/basic/app/app";
    }

    @ApiOperation("获取站点信息")
    @ApiImplicitParam(
            name = "appId",
            value = "站点ID",
            required = true,
            paramType = "path"
    )
    @GetMapping({"/get"})
    @ResponseBody
    public ResultData get() {
        int appId = BasicUtil.getInt("appId", -1);
        AppEntity app = null;
        if (appId < 0) {
            app = BasicUtil.getApp();
            if (app != null) {
                if (BasicUtil.getSession("appId") == null) {
                    BasicUtil.setSession("appId", app.getAppId());
                }
            } else {
                appId = (Integer)BasicUtil.getSession("appId");
                app = (AppEntity)this.appBiz.getById(appId);
            }
        } else {
            app = (AppEntity)this.appBiz.getById(appId);
        }

        return ResultData.build().success(app);
    }

    @ApiOperation("更新站点信息")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "appName",
            value = "应用名称",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "appDescription",
            value = "应用描述",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "appLogo",
            value = "应用logo",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "appDatetime",
            value = "站点日期",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "appKeyword",
            value = "网站关键字",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "appCopyright",
            value = "网站版权信息",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "appStyle",
            value = "网站采用的模板风格",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "appUrl",
            value = "网站域名",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "appManagerId",
            value = "管理站点的管理员id",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "appPayDate",
            value = "应用续费时间",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "appPay",
            value = "应用费用清单",
            required = false,
            paramType = "query"
    )})
    @PostMapping({"/update"})
    @LogAnn(
            title = "更新站点信息",
            businessType = BusinessTypeEnum.UPDATE
    )
    @RequiresPermissions({"basic:app:update"})
    @ResponseBody
    public ResultData update(@ModelAttribute @ApiIgnore AppEntity app, ModelMap mode, HttpServletRequest request, HttpServletResponse response) {
        mode.clear();
        ManagerEntity managerSession = BasicUtil.getManager();
        if (managerSession == null) {
            return ResultData.build().error();
        } else {
            mode.addAttribute("managerSession", managerSession);
            if (super.validated("app", "app_dir", app.getAppDir(), app.getId(), "id")) {
                return ResultData.build().error(this.getResString("err.exist", new String[]{this.getResString("app.dir")}));
            } else if (StringUtils.isBlank(app.getAppDir())) {
                return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("app.dir")}));
            } else if (!StringUtil.checkLength(app.getAppDir() + "", 0, 50)) {
                return ResultData.build().error(this.getResString("err.length", new String[]{this.getResString("app.dir"), "0", "10"}));
            } else {
                String cookie = BasicUtil.getCookie(CookieConstEnum.PAGENO_COOKIE);
                int pageNo = 1;
                if (StringUtils.isNotBlank(cookie) && Integer.valueOf(cookie) > 0) {
                    pageNo = Integer.valueOf(cookie);
                }

                mode.addAttribute("pageNo", pageNo);
                ResultData resultData = ResultData.build();
                if (!this.checkForm(app, resultData)) {
                    return resultData;
                } else {
                    if (StringUtils.isNotBlank(app.getAppLogo())) {
                        app.setAppLogo(app.getAppLogo().replace("|", ""));
                    }

                    app.setAppUrl(BasicUtil.getUrl());
                    MapCacheUtil.remove(BasicUtil.getDomain());
                    this.appBiz.updateById(app);
                    this.appBiz.updateCache();
                    return ResultData.build().success();
                }
            }
        }
    }

    @ApiOperation("刷新站点缓存")
    @PostMapping({"/refreshCache"})
    @ResponseBody
    public ResultData refreshCache(HttpServletRequest request) {
        MapCacheUtil.remove(BasicUtil.getDomain());
        this.appBiz.updateCache();
        return ResultData.build().success();
    }

    private boolean checkForm(AppEntity app, ResultData resultData) {
        if (StringUtils.isNotBlank(app.getAppKeyword()) && !StringUtil.checkLength(app.getAppKeyword(), 0, 1000)) {
            resultData.error(this.getResString("err.length", new String[]{this.getResString("appKeyword"), "0", "1000"}));
            return false;
        } else if (StringUtils.isNotBlank(app.getAppCopyright()) && !StringUtil.checkLength(app.getAppCopyright(), 0, 1000)) {
            resultData.error(this.getResString("err.length", new String[]{this.getResString("appCopyright"), "0", "1000"}));
            return false;
        } else if (StringUtils.isNotBlank(app.getAppDescription()) && !StringUtil.checkLength(app.getAppDescription(), 0, 1000)) {
            resultData.error(this.getResString("err.length", new String[]{this.getResString("appDescrip"), "0", "1000"}));
            return false;
        } else if (!StringUtil.checkLength(app.getAppName(), 1, 50)) {
            resultData.error(this.getResString("err.length", new String[]{this.getResString("appTitle"), "1", "50"}));
            return false;
        } else if (StringUtils.isNotBlank(app.getAppStyle()) && !StringUtil.checkLength(app.getAppStyle(), 1, 30)) {
            resultData.error(this.getResString("err.length", new String[]{this.getResString("appStyle"), "1", "30"}));
            return false;
        } else if (ObjectUtil.isNotNull(app.getAppHostUrl()) && !StringUtil.checkLength(app.getAppHostUrl(), 10, 150)) {
            resultData.error(this.getResString("err.length", new String[]{this.getResString("appUrl"), "10", "150"}));
            return false;
        } else {
            return true;
        }
    }

    @ApiOperation("判断是否有重复的域名")
    @GetMapping({"/checkUrl"})
    @ResponseBody
    public ResultData checkUrl(HttpServletRequest request) {
        if (request.getParameter("appUrl") != null) {
            return this.appBiz.countByUrl(request.getParameter("appUrl")) > 0 ? ResultData.build().success() : ResultData.build().error();
        } else {
            return ResultData.build().error();
        }
    }
}
